﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

using System.Diagnostics;
using MaterialDesignThemes.Wpf;
using CommunityToolkit.Mvvm.Messaging;
using System.Windows;
using houseAgency.Models;
using System.Timers;
using System.Collections.ObjectModel;
using _IMES.DBUtils;
using MySql.Data.MySqlClient;
using System.Data;
using houseAgency.View;
using System.Security.Policy;
using System.Reflection;
using houseAgency.Base;

namespace houseAgency.ViewModel
{
    public class MainViewModel : ObservableObject
    {
        public MainViewModel()
        {
            this.CurrentUserName = Global.currentLoginUser.Login_name;
            this.CurrentUserPower = Global.currentLoginUser.Login_power;

            Timer t1 = new(1000);
            t1.Elapsed += new ElapsedEventHandler(GetCurrentTime);
            t1.Enabled = true;
            t1.AutoReset = true;
            t1.Start();

            this.MenuList = GetMenuList(0);
        }

        private string currentUserName;

		public string CurrentUserName
		{
			get { return currentUserName; }
			set
			{
				currentUserName = value;
				OnPropertyChanged();
			}
		}

        private string currentUserPower;

        public string CurrentUserPower
        {
            get { return currentUserPower; }
            set
            {
                currentUserPower = value;
                OnPropertyChanged();
            }
        }

        private string loginTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

        public string LoginTime
        {
            get { return loginTime; }
            set
            {
                loginTime = value;
                OnPropertyChanged();
            }
        }

        private string currentTime;

        public string CurrentTime
        {
            get { return currentTime; }
            set
            {
                currentTime = value;
                OnPropertyChanged();
            }
        }

        private ObservableCollection<MenuNode> menuList;

        public ObservableCollection<MenuNode> MenuList
        {
            get { return menuList; }
            set
            {
                menuList = value;
                OnPropertyChanged();
            }
        }

        public ICommand SelectedItemChangedCommand
        {
            get
            {
                return new RelayCommand<MenuNode>(o =>
                {
                    if (o != null && o.ChildMenuList.Count == 0)
                    {
                        Global.title = o.Node;
                        WindowManager.Show(o.Url,null);
                    }
                });
            }
        }

        private void GetCurrentTime(object? sender, ElapsedEventArgs e)
        {
            this.CurrentTime = DateTime.Now.ToString("HH:mm:ss");
        }

        private ObservableCollection<MenuNode> GetMenuList(int parentID)
        {
            string sql = "select * from menu where pid = @pid";
            DataTable menuDataTable = MySQLHelper.ExecuteDataTable(MySQLHelper.DBConnectionString, sql,new MySqlParameter("pid",parentID));
            List<Menu> menus = DBConvert.DataTableToList<Menu>(menuDataTable);

            if (CurrentUserPower == "0")
            {
                Menu? menu = menus.FirstOrDefault(o => o.Name == "员工信息");
                if (menu != null) 
                { 
                    menus.Remove(menu) ;
                }
            }

            List<MenuNode> list = new List<MenuNode>();

            foreach (Menu menu in menus)
            {
                MenuNode menuNode = new MenuNode();
                menuNode.ID = menu.ID;
                menuNode.Node = menu.Name;
                menuNode.Url = menu.URL;
                menuNode.ChildMenuList = new ObservableCollection<MenuNode>(GetMenuList(menu.ID));
                list.Add(menuNode);
            }
            return new ObservableCollection<MenuNode>(list);
        }
    }
}
